//
//  OrderListHeaderView.m
//  much
//
//  Created by Believer Just on 2019/1/11.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "OrderListHeaderView.h"

@interface OrderListHeaderView ()

/** 内容的背景视图 */
@property (strong, nonatomic) UIView *contentBkgView;
/** 商家名字 */
@property (strong, nonatomic) UILabel *storeNameLabel;
/** 订单状态 */
@property (strong, nonatomic) UILabel *orderStatusLabel;

@end

@implementation OrderListHeaderView

+ (instancetype)initTableView:(UITableView *)tableView {
    OrderListHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([OrderListHeaderView class])];
    if (!headerView) {
        headerView = [[OrderListHeaderView alloc] initWithReuseIdentifier:NSStringFromClass([OrderListHeaderView class])];
    }
    
    return headerView;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = HEXCOLOR(0xf4f4f4);
    }
    return self;
}

- (void)setStoreName:(NSString *)storeName {
    _storeName = storeName;
    self.storeNameLabel.text = storeName;
}
- (void)setOrderState:(NSString *)orderState {
    _orderState = orderState;
    if (self.showGroupHeaderMessage) {
        [self setAttributedText:[NSString stringWithFormat:@"%@ %@", orderState, SCORE] iconFont:ICONFONT(13) lineOffset:0 cell:self.orderStatusLabel];
        self.orderStatusLabel.userInteractionEnabled = YES;
    } else {
        self.orderStatusLabel.text = orderState;
        self.orderStatusLabel.userInteractionEnabled = NO;
    }
}
- (void)setAttributedText:(NSString *)text iconFont:(UIFont *)iconFont lineOffset:(CGFloat)lineOffset cell:(UILabel *)label{
    // 同label不同字体大小
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = [text rangeOfString:@" "];
    [attrString addAttribute:NSFontAttributeName
                       value:APPFONT(12)
                       range:NSMakeRange(0, range.location)];
    [attrString addAttribute:NSFontAttributeName
                       value:iconFont
                       range:NSMakeRange(range.location, text.length - range.location)];
    [attrString addAttribute:NSBaselineOffsetAttributeName
                       value:@(lineOffset)
                       range:NSMakeRange(range.location, text.length - range.location)];
    [label setAttributedText:attrString];
}
- (void)setModel:(Orders *)model {
    _model = model;
    
    /**
    switch (_model.orderStateIndex) {
        case OrderListOrderStateTypePurchaseAuditPending:
            self.orderStatusLabel.text = @"待审核";
            self.orderStatusLabel.textColor = Color34;
            break;
        case OrderListOrderStateTypePurchaseAuditFail:
            self.orderStatusLabel.text = @"审核被拒";
            self.orderStatusLabel.textColor = Color175030;
            break;
        case OrderStateDeliveryPending:
            self.orderStatusLabel.text = @"审核通过，待发货";
            self.orderStatusLabel.textColor = Color34;
            break;
        case OrderStateReceivePending:
            self.orderStatusLabel.text = @"待收货";
            self.orderStatusLabel.textColor = Color34;
            break;
        case OrderStateSuccess:
            self.orderStatusLabel.text = @"已完成";
            self.orderStatusLabel.textColor = Color175030;
            break;
        default:
            break;
    }
     */
}


#pragma mark - 懒加载
- (UIView *)contentBkgView {
    if (!_contentBkgView) {
        _contentBkgView = [UIView new];
        _contentBkgView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_contentBkgView];
        [_contentBkgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
            make.top.mas_equalTo(@10);
            make.right.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView);
        }];
    }
    return _contentBkgView;
}
- (UILabel *)storeNameLabel {
    if (!_storeNameLabel) {
        UILabel * imgLab = [UILabel new];
        imgLab.text = N_STORE;
        imgLab.font = ICONFONT(16);
        imgLab.textColor = HEXCOLOR(0x676b75);
        [self.contentBkgView addSubview:imgLab];
        [imgLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentBkgView).with.offset(18);
            make.centerY.equalTo(self.contentBkgView);
            make.height.mas_equalTo(@40);
        }];
        
        _storeNameLabel = [UILabel new];
        _storeNameLabel.textColor = HEXCOLOR(0x353535);
        _storeNameLabel.font = APPFONT(12);
        [self.contentBkgView addSubview:_storeNameLabel];
        [_storeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imgLab.mas_right).with.offset(10);
            make.centerY.equalTo(imgLab);
            make.right.equalTo(self.orderStatusLabel.mas_left).with.offset(- 10);
        }];
        
        UIView * line = [UIView new];
        line.backgroundColor = HEXCOLOR(0xE4E4E4);
        [self.contentBkgView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imgLab);
            make.top.equalTo(self.contentBkgView.mas_bottom);
            make.right.equalTo(self.contentBkgView);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _storeNameLabel;
}
- (UILabel *)orderStatusLabel {
    if (!_orderStatusLabel) {
        _orderStatusLabel = [UILabel new];
        _orderStatusLabel.font = APPFONT(12);
        _orderStatusLabel.textColor = Color175030;
        _orderStatusLabel.textAlignment = NSTextAlignmentRight;
        _orderStatusLabel.userInteractionEnabled = NO;
        [self.contentBkgView addSubview:_orderStatusLabel];
        [_orderStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentBkgView).with.offset(-18);
            make.centerY.equalTo(self.storeNameLabel);
            make.width.mas_greaterThanOrEqualTo(@60);
        }];
        
        __weakConfigure
        [_orderStatusLabel tapUpWithBlock:^(UIView *v) {
            if (__weakSelf.showGroupHeaderBlock) {
                __weakSelf.showGroupHeaderBlock();
            }
        }];
    }
    return _orderStatusLabel;
}

@end
